/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-12-12     xu       the first version
 */
#define DBG_TAG "fs-init"
#define DBG_LVL DBG_LOG
#include <dfs_fs.h>
#include <dfs_posix.h>
#include <fal.h>
#include <rtdbg.h>

INIT_COMPONENT_EXPORT(fal_init); // initialize FAL, will call W25Qx_Init

static struct rt_device* fat_blk_dev = RT_NULL;

static int fat_blk_dev_create()
{
    /* Create a block device on the "fs" partition of spi flash */
    struct rt_device* fat_blk_dev = fal_blk_device_create(HTML_PART_NAME);
    if (fat_blk_dev == NULL) {
        LOG_E("Create block device for partition '%s' FAILED!", HTML_PART_NAME);
    } else {
        LOG_I("Create block device for partition '%s' OK!", HTML_PART_NAME);
    }
    return 0;
}
INIT_ENV_EXPORT(fat_blk_dev_create);

static int fat_blk_dev_mount()
{
    /* mount the file system from "fs" partition of spi flash. */
    if (dfs_mount(HTML_PART_NAME, "/", "elm", 0, 0) == 0) {
        LOG_I("File system mount OK!");
    } else {
        LOG_E("File system mount FAIL!");
    }

    return 0;
}
INIT_APP_EXPORT(fat_blk_dev_mount);
